import pygame
from pygame.locals import *

from code import InteractiveInterpreter

class CommandModel(InteractiveInterpreter):
    def __init__(self):
        InteractiveInterpreter.__init__(self)
        self.currentSource = ''
    def __getitem__(self,key):
        return self.locals[key]
    """
    def runcode(self,code):
        return InteractiveInterpreter.runcode(self,code)
    """
    def runsource(self,source):
        if not self.currentSource:
            returnValue = InteractiveInterpreter.runsource(self,source)
        else:
            returnValue = InteractiveInterpreter.runsource(
                self,
                self.currentSource)
        if returnValue:
            self.currentSource += source
        else:
            self.currentSource = ''
        return returnValue
    """
    def showsyntaxerror(self,filename=None):
        return InteractiveInterpreter.showsyntaxerror(self,filename)
    def showtraceback(self):
        return InteractiveInterpreter.showtraceback(self)
    def write(self,data):
        return InteractiveInterpreter.write(self,data)
    """

class CLICommandModel(CommandModel):
    def write(self,data):
        print '!! Caught data:',data

class GameCommandModel(CommandModel):
    def __init__(self,masterControl=None):
        CommandModel.__init__(self)
        self.masterControl = masterControl
    def write(self,data):
        self.masterControl.handleData(data)
    

class CommandController:
    def __init__(self,model,view):
        self.model = model
        self.view = view
    def processEvent(self,event):
        self.view.setCurrent

class CommandView(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.font = pygame.font.Font(None, 20)
        self.font.set_italic(1)
        self.color = Color('white')
        self.lastscore = -1
        self.update()
        self.rect = self.image.get_rect().move(10, 450)
    def update(self):
        if SCORE != self.lastscore:
            self.lastscore = SCORE
            msg = "Score: %d" % SCORE
            self.image = self.font.render(msg, 0, self.color)

class Game:
    screenRect = Rect(0, 0, 640, 480)
    windowStyle = 0  #|FULLSCREEN
    def __init__(self,windowStyle=windowStyle,):
        self.score = 0
        self.windowStyle = windowStyle 
    def Play(self):
        # Initialize pygame
        pygame.init()
        if pygame.mixer and not pygame.mixer.get_init():
            print 'Warning, no sound'
            pygame.mixer = None
        self.bestDepth = pygame.display.mode_ok(
            Game.screenRect.size,
            self.windowStyle,
            32,
            )
        screen = pygame.display.set_mode(
            Game.screenRect.size,
            self.windowStyle,
            self.bestDepth,
            )
        all = pygame.sprite.RenderUpdates()
        clock = pygame.time.Clock()
        while 1:
            #get input
            for event in pygame.event.get():
                if event.type == QUIT or \
                    (event.type == KEYDOWN and event.key == K_ESCAPE):
                        return
                elif event.type == KEYDOWN:
                    print event
                
            #draw the scene
            dirty = all.draw(screen)
            pygame.display.update(dirty)

            #cap the framerate
            clock.tick(40)
        if pygame.mixer:
            pygame.mixer.music.fadeout(1000)
        pygame.time.wait(1000)

if __name__=='__main__':
    game = Game()
    game.Play()
        
